﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using FlexCell;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YJKMaterialStatistic
{
	public partial class DlgColumnInfoSet : System.Windows.Forms.Form
	{
		public DlgColumnInfoSet(MEP_SPECIALITY speciality, string strGridName)
		{
			this.InitializeComponent();
			this.GetXYDpi(ref this.m_dpiX, ref this.m_dpiY);
			this.m_Speciality = speciality;
			this.m_strGridName = strGridName;
			if (this.m_strGridName == "材料表" || this.m_strGridName == "设备材料表" || this.m_strGridName == "主要设备、图形符号表")
			{
				this.chkLegend.Enabled = true;
			}
			else
			{
				this.chkLegend.Enabled = false;
			}
			switch (this.m_Speciality)
			{
			case MEP_SPECIALITY.Hvac:
				this.m_strFilePath = Path.Combine(Product.InstallLocation, "Templates", "HvacTempletlFile");
				break;
			case MEP_SPECIALITY.Plumbing:
				this.m_strFilePath = Path.Combine(Product.InstallLocation, "Templates", "PlumbingTempletlFile");
				break;
			case MEP_SPECIALITY.Electric:
				this.m_strFilePath = Path.Combine(Product.InstallLocation, "Templates", "ElectricTempletlFile");
				break;
			}
			this.m_strFilePath = this.m_strFilePath + "\\" + this.m_strGridName + ".hymb";
			this.GetLegend();
			this.chkLegend.Checked = this.m_gridSetInfo.Legend;
			this.SetGridTemplate();
			this.GetFontInfo();
			this.GetColumnInfo();
			foreach (KeyValuePair<string, short> keyValuePair in this.m_gridSetInfo.DicColumnWidth)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				dataGridViewRow.CreateCells(this.dataGridView1, new object[]
				{
					keyValuePair.Key,
					this.PixelToMM(keyValuePair.Value).ToString("0")
				});
				this.dataGridView1.Rows.Add(dataGridViewRow);
				dataGridViewRow.Cells[0].ReadOnly = true;
			}
			this.txtFontSize.Text = this.FontUnitToMM(this.m_gridSetInfo.FontSize).ToString("0");
			this.txtTitleFontSize.Text = this.FontUnitToMM(this.m_gridSetInfo.TitleFontSize).ToString("0");
			foreach (FontFamily fontFamily in FontFamily.Families)
			{
				this.cmbFontName.Items.Add(fontFamily.Name);
			}
			this.cmbFontName.Text = this.m_gridSetInfo.FontName;
			for (int j = 1; j < 17; j++)
			{
				this.cmbLineWidthOut.Items.Add(j.ToString());
				this.cmbLineWidthInner.Items.Add(j.ToString());
			}
			this.cmbLineWidthOut.Text = this.m_gridSetInfo.LineWidthOut.ToString();
			this.cmbLineWidthInner.Text = this.m_gridSetInfo.LineWidthInner.ToString();
			this.txtMargin.Text = this.m_gridSetInfo.Margin.ToString();
		}

		private void GetLegend()
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(this.m_strFilePath);
			foreach (object obj in xmlDocument.DocumentElement.SelectSingleNode("Cells").SelectNodes("Cell"))
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (xmlNode.InnerXml == "图例" || xmlNode.InnerText == "图形符号")
				{
					this.m_gridSetInfo.Legend = true;
					return;
				}
			}
			this.m_gridSetInfo.Legend = false;
		}

		private void GetFontInfo()
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(this.m_strFilePath);
			XmlElement documentElement = xmlDocument.DocumentElement;
			foreach (object obj in documentElement.SelectSingleNode("Cells").SelectNodes("Cell"))
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (xmlNode.InnerXml == "图例" || xmlNode.InnerText == "图形符号")
				{
					this.m_gridSetInfo.Legend = true;
					break;
				}
			}
			XmlElement xmlElement = documentElement.SelectSingleNode("GridInfo") as XmlElement;
			if (xmlElement == null)
			{
				return;
			}
			this.m_gridSetInfo.FontSize = Convert.ToSingle(xmlElement.GetAttribute("FontSize"));
			this.m_gridSetInfo.TitleFontSize = Convert.ToSingle(xmlElement.GetAttribute("TitleFontSize"));
			this.m_gridSetInfo.FontName = xmlElement.GetAttribute("FontName");
			this.m_gridSetInfo.LineWidthOut = Convert.ToInt32(xmlElement.GetAttribute("LineWidthOut"));
			this.m_gridSetInfo.LineWidthInner = Convert.ToInt32(xmlElement.GetAttribute("LineWidthInner"));
			this.m_gridSetInfo.Margin = Convert.ToInt32(xmlElement.GetAttribute("YuDu"));
		}

		private void SetGridTemplate()
		{
			if (!this.grid1.LoadFromXML(this.m_strFilePath))
			{
				this.grid1.Column(1).Width = 100;
				this.grid1.Cell(1, 1).Text = "表格预览加载失败!";
			}
			this.grid1.ScrollBars = ScrollBarsEnum.Both;
			this.grid1.Row(0).Visible = false;
			this.grid1.Column(0).Visible = false;
			this.grid1.Locked = true;
			this.grid1.ShowResizeTip = true;
			this.grid1.Refresh();
			this.grid1.BackColorBkg = System.Drawing.Color.White;
		}

		private void GetColumnInfo()
		{
			int num = 1;
			if (this.m_strGridName == "材料表")
			{
				this.m_gridSetInfo.DicColumnWidth.Add("序号", this.grid1.Column(num).Width);
				if (this.m_gridSetInfo.Legend)
				{
					this.m_gridSetInfo.DicColumnWidth.Add("图例", this.grid1.Column(++num).Width);
				}
				this.m_gridSetInfo.DicColumnWidth.Add("名称", this.grid1.Column(++num).Width);
				this.m_gridSetInfo.DicColumnWidth.Add("规格", this.grid1.Column(++num).Width);
				this.m_gridSetInfo.DicColumnWidth.Add("单位", this.grid1.Column(++num).Width);
				this.m_gridSetInfo.DicColumnWidth.Add("数量", this.grid1.Column(++num).Width);
				this.m_gridSetInfo.DicColumnWidth.Add("备注", this.grid1.Column(num + 1).Width);
				return;
			}
			switch (this.m_Speciality)
			{
			case MEP_SPECIALITY.Hvac:
				if (this.m_strGridName == "设备表")
				{
					this.m_gridSetInfo.DicColumnWidth.Add("序号", this.grid1.Column(1).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("名称", this.grid1.Column(2).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("型号（规格）", this.grid1.Column(3).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("材料", this.grid1.Column(4).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("件数", this.grid1.Column(5).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("单件", this.grid1.Column(6).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("合计", this.grid1.Column(7).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("备注", this.grid1.Column(7).Width);
					return;
				}
				if (this.m_strGridName == "设备材料表")
				{
					this.m_gridSetInfo.DicColumnWidth.Add("序号", this.grid1.Column(num).Width);
					if (this.m_gridSetInfo.Legend)
					{
						this.m_gridSetInfo.DicColumnWidth.Add("图例", this.grid1.Column(++num).Width);
					}
					this.m_gridSetInfo.DicColumnWidth.Add("名称", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("规格", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("单位", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("数量", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("备注", this.grid1.Column(num + 1).Width);
					return;
				}
				if (this.m_strGridName == "设备编号名称对照表")
				{
					this.m_gridSetInfo.DicColumnWidth.Add("编号", this.grid1.Column(1).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("用途及性能参数", this.grid1.Column(2).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("单位", this.grid1.Column(3).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("数量", this.grid1.Column(4).Width);
					return;
				}
				if (this.m_strGridName == "主要设备、图形符号表")
				{
					this.m_gridSetInfo.DicColumnWidth.Add("序号", this.grid1.Column(num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("名称", this.grid1.Column(++num).Width);
					if (this.m_gridSetInfo.Legend)
					{
						this.m_gridSetInfo.DicColumnWidth.Add("图例", this.grid1.Column(++num).Width);
					}
					this.m_gridSetInfo.DicColumnWidth.Add("型号（规格）", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("材料", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("件数", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("单件", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("合计", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("备注", this.grid1.Column(num + 1).Width);
					return;
				}
				break;
			case MEP_SPECIALITY.Plumbing:
				if (this.m_strGridName == "设备表")
				{
					this.m_gridSetInfo.DicColumnWidth.Add("序号", this.grid1.Column(1).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("名称", this.grid1.Column(2).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("型号（规格）", this.grid1.Column(3).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("材料", this.grid1.Column(4).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("件数", this.grid1.Column(5).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("单件", this.grid1.Column(6).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("合计", this.grid1.Column(7).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("备注", this.grid1.Column(7).Width);
					return;
				}
				if (this.m_strGridName == "设备材料表")
				{
					this.m_gridSetInfo.DicColumnWidth.Add("序号", this.grid1.Column(num).Width);
					if (this.m_gridSetInfo.Legend)
					{
						this.m_gridSetInfo.DicColumnWidth.Add("图例", this.grid1.Column(++num).Width);
					}
					this.m_gridSetInfo.DicColumnWidth.Add("名称", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("规格", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("单位", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("数量", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("备注", this.grid1.Column(num + 1).Width);
					return;
				}
				if (this.m_strGridName == "设备编号名称对照表")
				{
					this.m_gridSetInfo.DicColumnWidth.Add("编号", this.grid1.Column(1).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("用途及性能参数", this.grid1.Column(2).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("单位", this.grid1.Column(3).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("数量", this.grid1.Column(4).Width);
					return;
				}
				if (this.m_strGridName == "主要设备、图形符号表")
				{
					this.m_gridSetInfo.DicColumnWidth.Add("序号", this.grid1.Column(num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("名称", this.grid1.Column(++num).Width);
					if (this.m_gridSetInfo.Legend)
					{
						this.m_gridSetInfo.DicColumnWidth.Add("图例", this.grid1.Column(++num).Width);
					}
					this.m_gridSetInfo.DicColumnWidth.Add("型号（规格）", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("材料", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("件数", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("单件", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("合计", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("备注", this.grid1.Column(num + 1).Width);
					return;
				}
				break;
			case MEP_SPECIALITY.Electric:
				if (this.m_strGridName == "设备表")
				{
					this.m_gridSetInfo.DicColumnWidth.Add("序号", this.grid1.Column(1).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("名称", this.grid1.Column(2).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("型号及规格", this.grid1.Column(3).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("单位", this.grid1.Column(4).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("数量", this.grid1.Column(5).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("备注", this.grid1.Column(6).Width);
					return;
				}
				if (this.m_strGridName == "设备材料表")
				{
					this.m_gridSetInfo.DicColumnWidth.Add("序号", this.grid1.Column(num).Width);
					if (this.m_gridSetInfo.Legend)
					{
						this.m_gridSetInfo.DicColumnWidth.Add("图例", this.grid1.Column(++num).Width);
					}
					this.m_gridSetInfo.DicColumnWidth.Add("名称", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("规格", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("单位", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("数量", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("备注", this.grid1.Column(num + 1).Width);
					return;
				}
				if (this.m_strGridName == "主要设备、图形符号表")
				{
					this.m_gridSetInfo.DicColumnWidth.Add("序号", this.grid1.Column(num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("名称", this.grid1.Column(++num).Width);
					if (this.m_gridSetInfo.Legend)
					{
						this.m_gridSetInfo.DicColumnWidth.Add("图形符号", this.grid1.Column(++num).Width);
					}
					this.m_gridSetInfo.DicColumnWidth.Add("参照代号", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("型号及规格", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("单位", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("数量", this.grid1.Column(++num).Width);
					this.m_gridSetInfo.DicColumnWidth.Add("备注", this.grid1.Column(num + 1).Width);
				}
				break;
			default:
				return;
			}
		}

		private void GetXYDpi(ref double Xdpi, ref double Ydpi)
		{
			using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
			{
				Xdpi = (double)graphics.DpiX;
				Ydpi = (double)graphics.DpiY;
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.m_gridSetInfo.FontSize = this.MMToFontUnit(Convert.ToDouble(this.txtFontSize.Text));
			this.m_gridSetInfo.TitleFontSize = this.MMToFontUnit(Convert.ToDouble(this.txtTitleFontSize.Text));
			this.m_gridSetInfo.FontName = this.cmbFontName.Text;
			this.m_gridSetInfo.LineWidthOut = Convert.ToInt32(this.cmbLineWidthOut.Text);
			this.m_gridSetInfo.LineWidthInner = Convert.ToInt32(this.cmbLineWidthInner.Text);
			this.m_gridSetInfo.Margin = Convert.ToInt32(this.txtMargin.Text);
			this.m_gridSetInfo.TitleDown = this.chkTitleDown.Checked;
			for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
			{
				string key = this.dataGridView1.Rows[i].Cells[0].Value.ToString();
				string value = this.dataGridView1.Rows[i].Cells[1].Value.ToString();
				if (this.m_gridSetInfo.DicColumnWidth.ContainsKey(key))
				{
					this.m_gridSetInfo.DicColumnWidth[key] = this.MMToPixel(Convert.ToDouble(value));
				}
			}
			this.SaveGridTemplate();
			base.Close();
		}

		private void SaveGridTemplate()
		{
			if (this.m_gridSetInfo.DicColumnWidth.Count <= 0)
			{
				return;
			}
			this.grid1.Cell(1, 1).FontSize = this.m_gridSetInfo.TitleFontSize;
			this.grid1.Range(2, 1, this.grid1.Rows - 1, this.grid1.Cols - 1).FontSize = this.m_gridSetInfo.FontSize;
			this.grid1.Range(1, 1, this.grid1.Rows - 1, this.grid1.Cols - 1).FontName = this.m_gridSetInfo.FontName;
			Dictionary<string, short>.Enumerator enumerator = this.m_gridSetInfo.DicColumnWidth.GetEnumerator();
			int i = 0;
			while (enumerator.MoveNext())
			{
				i++;
				KeyValuePair<string, short> keyValuePair = enumerator.Current;
				this.grid1.Column(i).Width = keyValuePair.Value;
			}
			for (i = 1; i < this.grid1.Rows; i++)
			{
				this.grid1.Row(i).AutoFit();
			}
			this.grid1.ExportToXML(this.m_strFilePath);
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(this.m_strFilePath);
			XmlElement documentElement = xmlDocument.DocumentElement;
			XmlElement xmlElement = xmlDocument.CreateElement("GridInfo");
			documentElement.AppendChild(xmlElement);
			xmlElement.SetAttribute("FontSize", this.m_gridSetInfo.FontSize.ToString());
			xmlElement.SetAttribute("TitleFontSize", this.m_gridSetInfo.TitleFontSize.ToString());
			xmlElement.SetAttribute("FontName", this.m_gridSetInfo.FontName);
			xmlElement.SetAttribute("LineWidthOut", this.m_gridSetInfo.LineWidthOut.ToString());
			xmlElement.SetAttribute("LineWidthInner", this.m_gridSetInfo.LineWidthInner.ToString());
			xmlElement.SetAttribute("YuDu", this.m_gridSetInfo.Margin.ToString());
			xmlDocument.Save(this.m_strFilePath);
		}

		private double PixelToMM(short value)
		{
			return (double)value / this.m_dpiY * 0.08333 * 304.8;
		}

		private short MMToPixel(double value)
		{
			return Convert.ToInt16(value * this.m_dpiY / 0.08333 / 304.8);
		}

		private double FontUnitToMM(float value)
		{
			return (double)(value / 14f / 72f) * 304.8;
		}

		private float MMToFontUnit(double value)
		{
			return Convert.ToSingle(value * 14.0 * 72.0 / 304.8);
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void txtFontSize_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!char.IsNumber(e.KeyChar) && e.KeyChar != '\r' && e.KeyChar != '\b')
			{
				e.Handled = true;
			}
		}

		private void txtTitleFontSize_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!char.IsNumber(e.KeyChar) && e.KeyChar != '\r' && e.KeyChar != '\b')
			{
				e.Handled = true;
			}
		}

		private void txtMargin_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!char.IsNumber(e.KeyChar) && e.KeyChar != '\r' && e.KeyChar != '\b')
			{
				e.Handled = true;
			}
		}

		private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if (this.dataGridView1.CurrentCell.ColumnIndex != 0)
			{
				System.Windows.Forms.TextBox textBox = e.Control as System.Windows.Forms.TextBox;
				textBox.KeyPress -= this.tx_KeyPress;
				textBox.KeyPress += this.tx_KeyPress;
				return;
			}
			System.Windows.Forms.Control control = e.Control;
		}

		private void tx_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!char.IsNumber(e.KeyChar) && e.KeyChar != '\r' && e.KeyChar != '\b')
			{
				e.Handled = true;
			}
		}

		private void chkLegend_CheckedChanged(object sender, EventArgs e)
		{
			if (this.m_strFilePath == "" || this.m_gridSetInfo.DicColumnWidth.Count <= 0)
			{
				return;
			}
			this.m_gridSetInfo.Legend = this.chkLegend.Checked;
			string text = this.m_strFilePath;
			if (this.chkLegend.Enabled)
			{
				text = text.TrimEnd(".hymb".ToCharArray());
				if (this.m_gridSetInfo.Legend)
				{
					text += "（有图例）.hymb";
				}
				else
				{
					text += "（无图例）.hymb";
				}
				File.Copy(text, this.m_strFilePath, true);
			}
			this.grid1.LoadFromXML(this.m_strFilePath);
			this.grid1.Row(0).Visible = false;
			this.grid1.Column(0).Visible = false;
			this.grid1.Update();
			this.m_gridSetInfo.DicColumnWidth.Clear();
			this.GetColumnInfo();
			this.dataGridView1.Rows.Clear();
			foreach (KeyValuePair<string, short> keyValuePair in this.m_gridSetInfo.DicColumnWidth)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				dataGridViewRow.CreateCells(this.dataGridView1, new object[]
				{
					keyValuePair.Key,
					this.PixelToMM(keyValuePair.Value).ToString("0")
				});
				this.dataGridView1.Rows.Add(dataGridViewRow);
				dataGridViewRow.Cells[0].ReadOnly = true;
			}
		}

		private void DlgColumnInfoSet_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		private string m_strFilePath = "";

		private double m_dpiX;

		private double m_dpiY;

		private string m_strGridName = "";

		private MEP_SPECIALITY m_Speciality = MEP_SPECIALITY.Electric;

		public GridSetInfo m_gridSetInfo = new GridSetInfo();
	}
}
